#!/bin/sh
# 安装配置JDK 7
App=jdk
AppName=JDK
AppBase=/App
AppDir=$AppBase/jdk
AppSrcBase=/App/src
AppSrcFile=$App-*.tar.*
AppSrcDir=$(find $AppSrcBase -maxdepth 1 -name "$App*" -type d | sed 's/^.\///')

Community=JvmSnmp345
AllowHost=192.168.1.0/24

# 安装
finstall() {
    [ -d "$JAVA_HOME" ] && echo "$AppName 已安装" && exit

    cd $AppSrcBase
    [ -d "$AppSrcDir" ] && rm -rf $AppSrcDir

    tar zxf $AppSrcFile || tar jxf $AppSrcFile
    AppSrcDir=$(find $AppSrcBase -maxdepth 1 -name "$App*" -type d | sed 's/^.\///')
    mv $AppSrcDir $AppDir && echo "$AppName 安装成功" || echo "$AppName 安装失败"
}

# 初始化
finit() {
grep -q "JAVA_HOME" /etc/profile || cat >> /etc/profile << EOF
########################################
export JAVA_HOME=$AppDir
export JRE_HOME=\$JAVA_HOME/jre
export PATH=\$JAVA_HOME/bin:\$JRE_HOME/bin:\$PATH
export CLASSPATH=\$JAVE_HOME/lib:\$JRE_HOME/lib:./
EOF

cd $AppDir/jre/lib/management

cat > snmp.acl << EOF
acl = {
  {
    communities = $Community
    access = read-only
    managers = 127.0.0.1, $AllowHost
  }
}

trap = {
  {
    trap-community = $Community
    hosts = 127.0.0.1, $AllowHost
  }
}
EOF

cat > management.properties << EOF
com.sun.management.snmp.interface=0.0.0.0
com.sun.management.snmp.acl=true
com.sun.management.snmp.acl.file=$AppDir/jre/lib/management/snmp.acl
EOF

chmod 600 snmp.acl management.properties
[ $? -eq 0 ] && echo "初始化 $AppName 配置" || echo "初始化 $AppName 配置失败"
}

# 删除
fremove() {
    [ -d "$AppDir" ] && rm -rf $AppDir
    [ $? -eq 0 ] && echo "删除 $AppName" || echo "$AppName 未安装"
}


ScriptDir=$(dirname $0)
ScriptFile=$(basename $0)
case "$1" in
    "install"  ) finstall;;
    "remove"   ) fremove;;
    "reinstall") fremove && finstall;;
    "init"     ) finit;;
    * )
        echo "用法：$ScriptFile {install|remove|reinstall|init}"
        ;;
esac
